package com.project.baowei.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;



import com.project.baowei.bean.request.VerifyRequest;
import com.project.baowei.bean.respoes.LoginBean;
import com.project.baowei.bean.respoes.SelectBean;
import com.project.baowei.bean.respoes.VerifyBean;
import com.project.baowei.contract.HomeContract;
import com.project.baowei.dialog.NewLoadingDialog;
import com.project.baowei.dialog.SelectDialog;
import com.project.baowei.prsenter.HomePresenter;
import com.project.baowei.utils.UserUtils;
import com.szeasco.facesdk.icidconnection.nfc.NFCReadHelper;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;
import com.wangshen.projectbase.R;
import com.szeasco.facesdk.icidconnection.iccard.ICardRead;
import com.szeasco.facesdk.icidconnection.idcard.IDCardInfo;
import com.szeasco.facesdk.icidconnection.idcard.IDCardModuleType;
import com.szeasco.facesdk.icidconnection.idcard.IDCardUtilsKt;
import com.szeasco.facesdk.icidconnection.idcard.IReadIDCardCallback;
import com.szeasco.facesdk.icidconnection.idic.IDCardManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import com.szeasco.facesdk.icidconnection.nfc.NFCHelperTwo;
import com.szeasco.facesdk.icidconnection.nfc.NFCReadHelper;
import com.szeasco.facesdk.utils.ESFaceUtils;


/*@Route(path = AppActivityKey.HOMEACTIVITY)*/
public class HomeActivity extends BaseMvpActivity<HomePresenter> implements HomeContract.View {

    TextView tvChange,tvBind;

    Button loginBt;
    private boolean isRead = false;
    NewLoadingDialog newLoadingDialog;
     private final IDCardManager mIDCardManager = new IDCardManager();
    @Override
    public HomePresenter getPresenter() {
        return new HomePresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_home;
    }
    private String selectNo;
    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        newLoadingDialog=new NewLoadingDialog(mContext);
        tvChange=findViewById(R.id.tv_change);
        tvBind=findViewById(R.id.tv_bindNo);
        loginBt=findViewById(R.id.login_bt);
        try {
            List<LoginBean.OfficerDutyResultBean.WtArrangeResultListBean> listBeans= UserUtils.getInstance().getUserBean().getOfficerDutyResult().getWtArrangeResultList();
            if (listBeans.size() > 0){
                selectNo=listBeans.get(0).getId();
                tvBind.setText(listBeans.get(0).getDataName());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initLinsenterner() {
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*VerifyRequest verifyRequest=new VerifyRequest();
                verifyRequest.setIdCard("130982198912269213");
                verifyRequest.setArranges(selectNo);
                presenter.verify(verifyRequest);*/
                startReadIDCard();
            }
        });
        tvChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserUtils.getInstance().getUserBean().getOfficerDutyResult().getWtArrangeResultList()!= null && UserUtils.getInstance().getUserBean().getOfficerDutyResult().getWtArrangeResultList().size() == 1){
                    return;
                }
                List<SelectBean> selectBeans= null;
                try {
                    selectBeans = new ArrayList<>();
                    for (int i=0;i<UserUtils.getInstance().getUserBean().getOfficerDutyResult().getWtArrangeResultList().size();i++){
                        LoginBean.OfficerDutyResultBean.WtArrangeResultListBean bean=UserUtils.getInstance().getUserBean().getOfficerDutyResult().getWtArrangeResultList().get(i);
                        selectBeans.add(new SelectBean(bean.getId(),bean.getDataName()));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                SelectDialog selectDialog=new SelectDialog(mContext);
                selectDialog.setOnSelectClick(new SelectDialog.OnSelectClick() {
                    @Override
                    public void onSelect(SelectBean selectBean) {
                        //tvChange.setText(selectBean.getName());
                        selectNo=selectBean.getId();
                        tvBind.setText(selectBean.getName());
                    }
                });
                selectDialog.show(selectBeans,selectNo);

            }
        });
    }
    @Subscribe
    public void event(SelectBean selectBean){
        tvChange.setText(selectBean.getName());
    }
    @Override
    public void getData() {

    }

    @Override
    public void showMessage(String msg) {
        //super.showMessage(msg);
        View view= LayoutInflater.from(mContext).inflate(R.layout.layout_toast,null,false);
        TextView textView=view.findViewById(R.id.tv_title);
        textView.setText(msg);
        Toast toast=new Toast(mContext);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    @Override
    public void setData(VerifyBean verifyBean) {
        mIDCardManager.releaseReadIDCard();
        isRead = false;
        Bundle bundle=new Bundle();
        bundle.putSerializable("Bean",verifyBean);
        Intent intent=new Intent(mContext,VerifyActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        NFCReadHelper.INSTANCE.stopReadIDCard();
        //startIntent(AppActivityKey.VERIFYACTIVITY,bundle);
    }


    private void startReadIDCard() {
        newLoadingDialog.show();
        NFCReadHelper.INSTANCE.startReadIDCard(idCardInfo -> {
            //SoundPoolHelper.getInstance().playDi();
            runOnUiThread(() -> {

                newLoadingDialog.dismiss();
                String idCardStr = "姓名: " + idCardInfo.getUsername() + "\n" +
                        "性别: " + idCardInfo.getGender() + "\n" +
                        "名族: " + idCardInfo.getEthnic() + "\n" +
                        "地址: " + idCardInfo.getAddress() + "\n" +
                        "生日: " + idCardInfo.getBirthday() + "\n" +
                        "有效期: " + idCardInfo.getIdValidStart() + " - " + idCardInfo.getIdValid() + "\n" +
                        "发证机关: " + idCardInfo.getIdDepartment() + "\n" +
                        "证件号: " + idCardInfo.getIdNumber();
                //showMessage(idCardStr);
                VerifyRequest verifyRequest=new VerifyRequest();
                verifyRequest.setIdCard(idCardInfo.getIdNumber());
                verifyRequest.setArranges(selectNo);
                presenter.verify(verifyRequest);
            });
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        NFCReadHelper.INSTANCE.stopReadIDCard();
    }

/*
    @OnClick({R.id.tv_change, R.id.login_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_change:

                break;
            case R.id.login_bt:
                startReadIDCard();
                break;
        }
    }*/
}
